package com.yanqu.road.dao.impl.cross.party;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.BanquetRelationshipData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BanquetRelationDataDaoImpl extends TempDataObjectDao<BanquetRelationshipData> {
    public static String getCrossPartyRelationDataSql = "select * from `t_u_banquet_relation` where `lastJoinTime` > ?";
    public List<BanquetRelationshipData> getCrossPartyRelationData(long lastJoinTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<BanquetRelationshipData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getCrossPartyRelationDataSql);
            int i = 1;
            call.setLong(i++, lastJoinTime);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateCrossPartyRelationDataSql = "update `t_u_banquet_relation` set myJoinTimes=?,otherJoinTimes=?,lastJoinTime=?,otherUserBaseInfo = ? where myUserId=? and otherUserId=? and partyType=? ";
    @Override
    protected boolean updateData(BanquetRelationshipData crossPartyRelationData) {
        crossPartyRelationData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossPartyRelationData.getMyJoinTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPartyRelationData.getOtherJoinTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, crossPartyRelationData.getLastJoinTime()));
        para.put(i++, new DbParameter(Types.NVARCHAR, JSON.toJSONString(crossPartyRelationData.getOtherUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, crossPartyRelationData.getMyUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossPartyRelationData.getOtherUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPartyRelationData.getPartyType()));
        if (getDb().executeNoneQuery(updateCrossPartyRelationDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossPartyRelationDataSql = "replace into `t_u_banquet_relation`(myUserId,myJoinTimes,otherUserId,otherJoinTimes,partyType,lastJoinTime,otherUserBaseInfo) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(BanquetRelationshipData crossPartyRelationData) {
        crossPartyRelationData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossPartyRelationData.getMyUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPartyRelationData.getMyJoinTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, crossPartyRelationData.getOtherUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPartyRelationData.getOtherJoinTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, crossPartyRelationData.getPartyType()));
        para.put(i++, new DbParameter(Types.BIGINT, crossPartyRelationData.getLastJoinTime()));
        para.put(i++, new DbParameter(Types.NVARCHAR, JSON.toJSONString(crossPartyRelationData.getOtherUserBaseInfo())));
        if (getDb().executeNoneQuery(insertCrossPartyRelationDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected BanquetRelationshipData parseData(ResultSet rs) throws SQLException {
        BanquetRelationshipData crossPartyRelationData = new BanquetRelationshipData();
        crossPartyRelationData.setMyUserId(rs.getLong("myUserId"));
        crossPartyRelationData.setMyJoinTimes(rs.getInt("myJoinTimes"));
        crossPartyRelationData.setOtherUserId(rs.getLong("otherUserId"));
        crossPartyRelationData.setOtherJoinTimes(rs.getInt("otherJoinTimes"));
        crossPartyRelationData.setPartyType(rs.getInt("partyType"));
        crossPartyRelationData.setLastJoinTime(rs.getLong("lastJoinTime"));

        String otherUserBaseInfoStr = rs.getString("otherUserBaseInfo");
        if (StringUtils.isNullOrEmpty(otherUserBaseInfoStr) || otherUserBaseInfoStr.equals("null")) {
            crossPartyRelationData.setOtherUserBaseInfo(new UserBaseInfo());
        } else {
            crossPartyRelationData.setOtherUserBaseInfo(JSON.parseObject(otherUserBaseInfoStr, UserBaseInfo.class));
        }

        return crossPartyRelationData;
    }
}
